﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using SST.WebClient.Util;
using SST.WebClient.Data;

namespace SST.Phone.Util
{
    /// <summary>
    /// Functions to ease lives in silverlight
    /// </summary>
    public class UIHelper
    {
        #region Debugger
        public static void Debug(string message)
        {
            System.Diagnostics.Debug.WriteLine(message);
        }
        public static void Debug(Exception ex)
        {
            Debug(ex.Message + "\r\n" + ex.StackTrace);
        }
        public static void Debug(string message, Exception ex)
        {
            Debug(message + "=>" + ex.Message + "\r\n" + ex.StackTrace);
        }
        #endregion
    }

    /// <summary>
    /// Converter for Image to consume string url
    /// </summary>
    public class UIImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return null;
            //treat url start with slash, belongs to content of this application, use UriKind.Relative
            try
            {
                return new BitmapImage(new Uri((string)value, ((string)value).StartsWith("/") ? UriKind.Relative : UriKind.Absolute));
            }
            catch (Exception ex)
            {
                UIHelper.Debug(ex);
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Converter for uri to consume string url
    /// </summary>
    public class UIUriConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return null;
            //treat url start with slash, belongs to content of this application, use UriKind.Relative
            try
            {
                return new Uri((string)value, ((string)value).StartsWith("/") ? UriKind.Relative : UriKind.Absolute);
            }
            catch (Exception ex)
            {
                UIHelper.Debug(ex);
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Converter for track duration seconds to minute:second format
    /// </summary>
    public class UIDurationConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return null;
            try
            {
                TimeSpan span = new TimeSpan(0, 0, (int)value);
                return string.Format(" ({0:0}:{1:00})", span.Minutes, span.Seconds);
            }
            catch (Exception ex)
            {
                UIHelper.Debug(ex);
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Converter for track title + track duration seconds to minute:second format
    /// </summary>
    public class UITrackTitleAndDurationConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return null;
            try
            {
                UIDurationConverter durationConverter = new UIDurationConverter();
                return ((Soundtrack)value).Title + " " + durationConverter.Convert(((Soundtrack)value).Duration, typeof(string), parameter, culture);
            }
            catch (Exception ex)
            {
                UIHelper.Debug(ex);
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Converter for showing "requester: comment"
    /// </summary>
    public class UIRequesterCommentConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return null;
            try
            {
                string display = ((StreamingRequest)value).Requester;
                if (((StreamingRequest)value).Comment != null)
                    display += ": " + ((StreamingRequest)value).Comment;
                return display;
            }
            catch (Exception ex)
            {
                UIHelper.Debug(ex);
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Converter bool to visibility
    /// </summary>
    public class UIVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return null;
            try
            {
                if ((bool)value)
                    return Visibility.Visible;
                else
                    return Visibility.Collapsed;
            }
            catch (Exception ex)
            {
                UIHelper.Debug(ex);
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Converter for interchanging chat refresh list selectedIndex to seconds
    /// </summary>
    public class UIChatRefreshTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return 0;
            try
            {
                int seconds = (int)value;
                switch (seconds)
                {
                    case 0:
                        return 0;
                    case 15:
                        return 1;
                    case 30:
                        return 2;
                    case 45:
                        return 3;
                    case 60:
                        return 4;
                    case 300:
                        return 5;
                    case 600:
                        return 6;
                }
            }
            catch (Exception ex)
            {
                UIHelper.Debug(ex);
            }
            return 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return 0;
            try
            {
                int seconds = (int)value;
                switch (seconds)
                {
                    case 0:
                        return 0;
                    case 1:
                        return 15;
                    case 2:
                        return 30;
                    case 3:
                        return 45;
                    case 4:
                        return 60;
                    case 5:
                        return 300;
                    case 6:
                        return 600;
                }
            }
            catch (Exception ex)
            {
                UIHelper.Debug(ex);
            }
            return 0;
        }
    }
}
